<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Artist extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();

		//Google Analytics ID
		$this->data['ga_id'] = 12;
				
		//Load model
		$this->load->model('artist_model');
		
		//Redirect alias name
		$this->artist_model->redirect_artist_name_alias();
				
		//Load Video Script
		$this->data['js'][] = "artistScript.js";
		$this->data['css'][] = "artistStyle.css";
		
		$this->data['artist_view_mode'] = get_cookie('artist_view_mode');
		if(empty($this->data['artist_view_mode'])) $this->data['artist_view_mode'] = 3;
		
		 //Load Library
  		$this->load->library('pagination');	
		
		//Load Top Sub Menu
		$this->data['top_sub_menu'] = $this->top_sub_menu();
		
		//Set SEO Tag
		$this->data['seo_title'] = 'ประวัติศิลปิน ศิลปินใหม่ ดารา นักร้อง ทั้งศิลปินไทยและศิลปินต่างประเทศ | You2Play.com';
		$this->data['seo_description'] = 'ประวัติศิลปิน ศิลปินใหม่ ดารา นักร้อง ทั้งศิลปินไทยและศิลปินต่างประเทศ แบ่งตามแนวเพลงของศิลปิน จัดอันดับศิลปิน';
		$this->data['seo_keyword'] = 'ประวัติศิลปิน, ศิลปินใหม่, ศิลปินไทย, ศิลปินต่างประเทศ, ดารา, นักร้อง';	
    }
	
	/*Top Menu*/
	private function top_sub_menu()
	{
		//Filter Type
		$this->data['type'] = $this->uri->segment(1);
		if(!in_array($this->data['type'],array('new_released','recommend','hit'))) $this->data['type'] = "new_released";
		//Filter Media Type
		switch($this->uri->segment(2)):
			case 'thai':
				$this->data['media_type'] = 'thai';
				$this->data['media_type_text'] = 'ศิลปินไทย';
			break;
			case 'inter':
				$this->data['media_type'] = 'inter';
				$this->data['media_type_text'] = 'ศิลปินสากล';
			break;
			case 'asian':
				$this->data['media_type'] = 'asian';
				$this->data['media_type_text'] = 'ศิลปินเอเชียน';
			break;
			default:
				$this->data['media_type'] = 'all';
				$this->data['media_type_text'] = 'ศิลปินทั้งหมด';
		endswitch;
		//Filter Genre
		$this->data['genre'] = $this->uri->segment(3);
		$is_exist = $this->db->from('genre')->where(array('genre_alias' => ''.$this->data['genre'].''))->count_all_results();
		if(!empty($is_exist)):
			$dataGenre = $this->db->select('genre_name')->get_where('genre',array('genre_alias' => $this->data['genre']))->row();
			$this->data['genre_text'] = $dataGenre->genre_name;				
		else:
			$this->data['genre'] = "all";	
			$this->data['genre_text'] = "แนวเพลงทั้งหมด";			
		endif;
		
		$this->data['dataGenre'] = $this->db
		->order_by('genre_name ASC')
		->get('genre')
		->result();		
			
		return $this->load->view($this->router->class."/top_sub_menu",$this->data, TRUE);
	}
	
	//Keep View Mode
	public function ajax_set_cookies(){
		$artist_view_mode = $this->input->post('artist_view_mode');
		if(empty($artist_view_mode)) $this->page_missing();
		$mycookie = array(
		   'name'  => 'artist_view_mode',
		   'value' => $artist_view_mode,
		   'expire'=> '86500'
  		);
		set_cookie($mycookie);
	}	
		
	/*Home*/	 
	public function index()
	{	
		if($this->input->post('ajax')) $this->ajax_artist_pager();
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Nova Slider
/*		$this->data['js'][] = "jquery.nivo.slider.pack.js";
		$this->data['css'][] = "nivo-slider.css";
		$this->data['css'][] = "themes/default/default.css";*/
	
		//Check Artist View Mode
		$this->data['artist_view_mode'] = get_cookie('artist_view_mode');		
		if(empty($this->data['artist_view_mode'])) $this->data['artist_view_mode'] = 3;	
		
		if(!intval($this->input->get('p'))):
			if ( !$this->data['dataArtistList'] = $this->memcache->get('dataArtistList')):
				$this->data['dataArtistList'] = $this->ajax_artist_pager('Search');
				//Cache Set
				$this->memcache->set('dataArtistList', $this->data['dataArtistList'], $this->memcache_time);		
			endif;
		else:
			$this->data['dataArtistList'] = $this->ajax_artist_pager('Search');
		endif;	
		
		$this->data['content'] = $this->router->class."/index";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	private function ajax_artist_pager($mode,$ajax = FALSE)
	{
		switch($mode):
			case 'Search':
				$text = $this->input->get('text');
				$letter = $this->input->get('letter');
				$per_page = 32;
				$searchCondition = "t.is_publish = '1' AND (t.artist_publish_date IS NULL OR t.artist_publish_date < NOW())";
				if(!empty($letter)):
					if($letter == "other_thai"):
						$searchCondition.= " AND t.letters = '#' AND t.media_type_id = 1";
					elseif($letter == "other_eng"):
						$searchCondition.= " AND t.letters = '#' AND t.media_type_id IN (2,3)";
					else:
						$searchCondition.= " AND t.letters = '".$letter."'";
					endif;
				endif;
				if(!empty($text)):
					$searchCondition.= " AND j.artist_name LIKE '%".$text."%'";
				endif;  
				$order_by = "t.artist_publish_date DESC,t.artist_id DESC";
			break;
			case 'Filter':
				$per_page = 32;
				$type = $this->uri->segment(1);
				$media_type = $this->uri->segment(2);
				$genre = $this->uri->segment(3);
				$language = $this->uri->segment(4);
				$alphabet = $this->uri->segment(5);
				$searchCondition = "t.is_publish = '1' AND (t.artist_publish_date IS NULL OR t.artist_publish_date < NOW())";
				$order_by = "";
				if(!empty($type)) switch($type):	
					case 'new_released':
						$order_by .= "t.artist_publish_date DESC,t.artist_id DESC";
					break;			
					case 'hit':
						$searchCondition .= " AND DATE(t.artist_publish_date) >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH)";
						$order_by = "t.views DESC";
					break;
					case 'recommend':
						$searchCondition .= " AND is_show_recommend = '1'";
						$order_by = "t.artist_publish_date DESC,t.artist_id DESC";
					break;					
				endswitch;
				if(!empty($media_type) && $media_type != "all"): 
					switch($media_type):	
						case 'thai':
							$searchCondition .= " AND t.media_type_id = 1";
						break;			
						case 'inter':
							$searchCondition .= " AND t.media_type_id = 2";
						break;
						case 'asian':
							$searchCondition .= " AND t.media_type_id = 3";
						break;					
					endswitch;
				endif;
				if(!empty($genre) && $genre != "all"):
					$is_exist = $this->db->from('genre')->where(array('genre_alias' => $genre))->count_all_results();
					if(empty($is_exist)) $this->page_missing();
					$searchCondition .= " AND l.genre_alias = '".$genre."'";
				endif;
				if(!empty($language) && !empty($alphabet)):
					if($alphabet == "other") $alphabet = "#";
					if($language == "e" && $media_type == "thai"):
						$searchCondition = "t.is_publish = '1' AND (t.media_type_id = 2 OR t.media_type_id = 3) AND t.letters = '".$alphabet."'";
					elseif($language == "t" && ($media_type == "inter" || $media_type == "asian")):
						$searchCondition = "t.is_publish = '1' AND t.media_type_id = 1 AND t.letters = '".$alphabet."'";
					else:
						$searchCondition .= " AND t.letters = '".urldecode($alphabet)."'";
					endif;
				endif;		
			break;
		endswitch;
				
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->select('COUNT(DISTINCT(t.artist_id)) as Total')
		->join('artist_genre as k','k.artist_id=t.artist_id','INNER')
		->join('genre as l','l.genre_id = k.genre_id','INNER')		
		->join('artist_info as j',"j.artist_id=t.artist_id",'INNER')
		->get_where('artist as t',$searchCondition)
		->row()->Total;	
	   	$config['per_page'] = $per_page;
	   	$config['num_links'] = 5;
		$config['first_link'] = '<span class="arrows first ">&laquo;</span>';
  		$config['next_link'] = '<span class="arrows next ">&gt;</span>';
  		$config['prev_link'] = '<span class="arrows prev ">&lt;</span>';
  		$config['last_link'] = '<span class="arrows last ">&raquo;</span>';
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
	
		$this->data['dataArtistList'] = $this->db->select('t.artist_id,j.artist_name')
		->join('artist_genre as k','k.artist_id=t.artist_id','INNER')
		->join('genre as l','l.genre_id = k.genre_id','INNER')		
		->join('artist_info as j',"j.artist_id=t.artist_id",'INNER')
		->order_by($order_by)
		->group_by('t.artist_id')
		->get_where('artist as t',$searchCondition,$config['per_page'],intval($this->input->get('p')))->
		result();	
		foreach($this->data['dataArtistList'] as $value):
			$value->image = $this->db->order_by('artist_image_order ASC,artist_image_id ASC')
			->get_where('artist_image',array('artist_id' => $value->artist_id),1)
			->row();
		endforeach;

		if($ajax):
			echo $this->load->view($this->router->class."/ajax_artist_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_artist_pager", $this->data, TRUE);		
		endif;		
	}

	public function new_released(){$this->filter('new_released');}
	public function hit(){$this->filter('hit');}
	public function recommend(){$this->filter('recommend');}
	private function filter($type = NULL)
	{
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";	
				
		//Load Module
		$this->data['module'] = $this->module('artist');
		
		//Check Mode
		$this->data['artist_view_mode'] = get_cookie('artist_view_mode');
		if(empty($this->data['artist_view_mode'])) $this->data['artist_view_mode'] = 3;	
		
		$type = $this->uri->segment(1);
		$media_type = $this->uri->segment(2);
		$genre = $this->uri->segment(3);
		$language = $this->uri->segment(4);
		$alphabet = $this->uri->segment(5);
		switch($type):
			case 'new_released':
				if(!intval($this->input->get('p')) && $media_type == 'all' && $genre == 'all' && $language == NULL && $alphabet == NULL):
					if ( !$this->data['dataArtistList'] = $this->memcache->get('dataArtistNewRelease')):
						$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
						//Cache Set
						$this->memcache->set('dataArtistNewRelease', $this->data['dataArtistList'], $this->memcache_time);		
					endif;		
				else:
					$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
				endif;	
				
				//Set SEO Tag
				$this->data['seo_title'] = 'ศิลปินใหม่ | You2Play.com';
				$this->data['seo_keyword'] = 'ศิลปินใหม่, ประวัติศิลปิน, ประวัติดารา';
				
				//GA Events
				$this->data['ga_event'] = 'CI-'."new_released";						
			break;
			case 'hit':
				if(!intval($this->input->get('p')) && $media_type == 'all' && $genre == 'all' && $language == NULL && $alphabet == NULL):
					if ( !$this->data['dataArtistList'] = $this->memcache->get('dataArtistHit')):
						$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
						//Cache Set
						$this->memcache->set('dataArtistHit', $this->data['dataArtistList'], $this->memcache_time);		
					endif;		
				else:
					$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
				endif;
				
				//Set SEO Tag
				$this->data['seo_title'] = 'ศิลปินฮิต | You2Play.com';
				$this->data['seo_keyword'] = 'ศิลปินใหม่, ประวัติศิลปิน, ประวัติดารา';
				
				//GA Events
				$this->data['ga_event'] = 'CI-'."hit";							
			break;
			case 'recommend':
				if(!intval($this->input->get('p')) && $media_type == 'all' && $genre == 'all' && $language == NULL && $alphabet == NULL):
					if ( !$this->data['dataArtistList'] = $this->memcache->get('dataArtistRecommend')):
						$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
						//Cache Set
						$this->memcache->set('dataArtistRecommend', $this->data['dataArtistList'], $this->memcache_time);		
					endif;		
				else:
					$this->data['dataArtistList'] = $this->ajax_artist_pager('Filter');
				endif;
			
				//Set SEO Tag
				$this->data['seo_title'] = 'ศิลปินแนะนำ | You2Play.com';
				$this->data['seo_keyword'] = 'ศิลปินใหม่, ประวัติศิลปิน, ประวัติดารา';
				
				//GA Events
				$this->data['ga_event'] = 'CI-'."recommend";								
			break;						
		endswitch;
				
		$this->data['content'] = $this->router->class."/filter";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}	
	
	/*Search*/
	public function search()
	{	
		//Load Module
		$this->data['module'] = $this->module('music');			
		
		//Chords Search
		$this->data['dataArtistList'] = $this->ajax_artist_pager('Search');		
		
		$this->data['content'] = $this->router->class."/filter";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}	
	
	
	
	
	
	
	
		
	
	/*ajax_search*/
	public function ajax_search()
	{
		$txt = $this->input->post('txt');
		$this->data['searchAuto'] = $this->db->select('t.artist_name,t.artist_id')->
		join('artist as j',"j.artist_id=t.artist_id",'INNER')->
		where("t.artist_name LIKE '".$txt."%'")->
		get('artist_info as t',10)->
		result();
		
		$this->load->view("artist/ajax_search", $this->data);
	}
	public function ajax_search_result()
	{
		$this->load->library('solr');
		$txt = $this->solr->escapeSolrValue($this->input->get('txt'));
		$SQL = 'artist_name_s:'.$txt.'* OR artist_name_s:'.$txt;		

		$config['base_url'] = base_url().'ajax_search_result?txt='.$txt.query_string(array('per_page','txt'));
		$config['total_rows'] = $this->solr->query($SQL)->count_all_results();
		$config['per_page'] = 20;
		$config['num_links'] = 5;
		$config['first_link'] = '<span class="arrows first">&laquo;</span>';
  		$config['next_link'] = '<span class="arrows next">&gt;</span>';
  		$config['prev_link'] = '<span class="arrows prev">&lt;</span>';
  		$config['last_link'] = '<span class="arrows last">&raquo;</span>';
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();

		$this->data['dataResult'] = $this->solr->query($SQL)->limit(intval($this->input->get('p')),$config['per_page'])->
		order_by(array('last_modified' => 'DESC'))->result();
		foreach($this->data['dataResult'] as $value):
			$value->artist_id = substr($value->id,7);
			/*Get Artist Info*/
			if(!empty($value->artist_id_i_arr)):
				$artistQuery = '';
				if(is_array($value->artist_id_i_arr)):
					foreach($value->artist_id_i_arr as $artist_id):
						$artistQuery .= 'id:artist_'.$artist_id.' OR ';
					endforeach;
					$artistQuery = rtrim($artistQuery,' OR ');
				else:
					$artistQuery = 'id:artist_'.$value->artist_id_i_arr;
				endif;
				$value->artist = $this->solr->query($artistQuery)->result();
			endif;
			/*Get Album Info*/
			if(!empty($value->album_id_i_arr)):
				$albumQuery = '';
				if(is_array($value->album_id_i_arr)):
					foreach($value->album_id_i_arr as $album_id):
						$albumQuery .= 'id:album_'.$album_id.' OR ';
					endforeach;
					$albumQuery = rtrim($artistQuery,' OR ');
				else:
					$albumQuery = 'id:album_'.$value->album_id_i_arr;
				endif;
				$value->album = $this->solr->query($albumQuery)->result();
			endif;
		endforeach;
		
		$this->load->view("artist/ajax_search_result", $this->data);
	}
	
	/*Info Header*/
	private function infoHeader($artist_id)
	{
		//Helper Text
		$this->load->helper('text');
		
		$is_exist = $this->db->from('artist')->where(array('artist_id' => $artist_id))->count_all_results();
		if(empty($is_exist)) $this->page_missing();
		
		//UPDATE Artist (View)
		$this->db->set('views', 'views + 1', FALSE);
		$this->db->where(array('artist_id' => $artist_id));
		$this->db->update('artist');
		
		//Keep Stat View
		$datetime = date('Y-m-d');
		$is_exist = $this->db->from('artist_click_stat')->where(array('artist_id' => $artist_id,'update_date' => $datetime))->count_all_results();
		if(!empty($is_exist)):
			$this->db->set('artist_click_views', 'artist_click_views + 1', FALSE);
			$this->db->where(array('artist_id' => $artist_id,'update_date' => $datetime));
			$this->db->update('artist_click_stat');
		else:
			$data = array(
				'artist_id' => $artist_id,
				'artist_click_views' => 1,
				'update_date' => $datetime
			);
			$this->db->insert('artist_click_stat',$data);
		endif;				
		
		$this->data['dataArtist'] = $this->db->select('t.artist_id,t.rate_score,j.artist_name,j.artist_short_desc,j.artist_desc,j.genre_tag,j.artist_tag,m.label_name,l.media_type_name')
		->join('artist_info as j','j.artist_id=t.artist_id','INNER')
		->join('media_type as l','l.media_type_id=t.media_type_id','LEFT')
		->join('label as m','m.label_id=t.label_id','LEFT')		
		->get_where('artist as t',array('t.artist_id' => $artist_id),1)
		->row();
		$this->data['dataArtist']->image = $this->db->order_by('artist_image_order ASC,artist_image_id ASC')
		->get_where('artist_image',array('artist_id' => $artist_id),1)
		->row();
		$this->data['artist_seo_name'] = seo_url($this->data['dataArtist']->artist_name);
		//Total Artist Click Rate
		$this->data['dataArtist']->artist_rate_all = @$this->db->from('artist_rating')
		->where(array('artist_id' => $artist_id))
		->count_all_results();

		$this->data['module_facebook_plugins_share'] = $this->module_facebook_plugins_share();
		$this->data['module_facebook_plugins_comment'] = $this->module_facebook_plugins_comment(base_url().'summary/'.$artist_id.'-'.$this->data['artist_seo_name']);
		
		//Set SEO Tag
		$this->data['seo_title'] = 'ศิลปิน '.$this->data['dataArtist']->artist_name.' | ประวัติศิลปิน ดารา นักร้อง You2Play.com';
		$this->data['seo_description'] = 'ศิลปิน '.$this->data['dataArtist']->artist_name.' '.$this->data['seo_description'];	
		$this->data['seo_keyword'] = ($this->data['dataArtist']->artist_tag ? rtrim($this->data['dataArtist']->artist_tag,','):'').', ประวัติศิลปิน, ดารา, นักร้อง';
		$this->data['fb_image'] = image_asset_url('../uploads/artist/images/305x305/'.$this->data['dataArtist']->image->artist_file_name.'.'.$this->data['dataArtist']->image->artist_file_ext);
		
		//GA Events
		$this->data['ga_event'] = 'CI-'.$artist_id.'-'.$this->data['artist_seo_name'];						
				
		return $this->load->view($this->router->class."/info_header", $this->data, true);
	}
	
	/*Ajax Vote Artist*/
	public function ajax_vote_artist()
	{
		$this->artist_model->ajax_vote_artist();
	}	
	
	/*Summary*/
	public function summary($artist_id = NULL,$seo_text = NULL)
	{
		$is_exist = $this->db->from('artist')->where(array('artist_id' => $artist_id,'is_publish' => '1'))->count_all_results();
		if(empty($is_exist)) $this->page_missing();		
		
		//Rediect if no seo text
		if(empty($seo_text)):
			$dataResult = $this->db->get_where('artist_info',array('artist_id' => $artist_id),1)->row();
			redirect($artist_url.'summary/'.intval($artist_id).'-'.seo_url($dataResult->artist_name), 'location', 301);
		endif;			
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
		
		$this->data['dataSong'] = $this->db->select('j.song_id,j.song_name')
		->join('song_info as j','j.song_id=t.song_id','INNER')
		->join('song as k','k.song_id=t.song_id','INNER')
		->order_by('k.song_publish_date DESC,j.song_id DESC')
		->get_where('song_artist as t',array('t.artist_id' => $artist_id),10)
		->result();
		foreach($this->data['dataSong'] as $value):
			$value->video = $this->db->select('t.video_id,j.video_name')
			->join('video_info as j','j.video_id=t.video_id','INNER')
			->get_where('video_song as t',array('t.song_id' => $value->song_id),1)
			->row();
		endforeach;
		
		$this->data['dataAlbum'] = $this->db->select('k.album_id,k.album_name,k.album_file_name,k.album_file_ext')
		->join('song_album as j','j.song_id=t.song_id','INNER')
		->join('album_info as k','k.album_id=j.album_id','INNER')
		->group_by('j.album_id')
		->order_by('j.album_id','DESC')	
		->get_where('song_artist as t',array('t.artist_id' => $artist_id),5)
		->result();

		$this->data['dataVideo'] = $this->db->select('t.video_id,t.views,m.video_name,m.video_thumb_name,m.video_thumb_ext')
		->join('video_song as j','j.video_id=t.video_id','LEFT')
		->join('video_artist as k','k.video_id=t.video_id','LEFT')
		->join('song_artist as l','l.song_id=j.song_id','LEFT')
		->join('video_info as m','m.video_id=t.video_id','INNER')
		->order_by('t.video_id','DESC')
		->get_where('video as t',"k.artist_id = ".$artist_id." OR l.artist_id = ".$artist_id."",4)
		->result();
		
		$this->data['dataNews'] = $this->db->select('j.news_id,j.news_subject,j.news_brief')
		->join('news as k','k.news_id=t.news_id','INNER')
		->join('news_info as j','j.news_id=t.news_id','INNER')	
		->order_by('j.news_id','DESC')		
		->get_where('news_artist as t',"t.artist_id = $artist_id AND k.is_publish = '1' AND (k.news_publish_date IS NULL OR k.news_publish_date < NOW())",4)
		->result();	
		foreach($this->data['dataNews'] as $value):
			$dataImage = $this->db->order_by('news_image_order','ASC')->get_where('news_image',array('news_id' => $value->news_id),1)->row();
			$value->news_file_name = @$dataImage->news_file_name;
			$value->news_file_ext = @$dataImage->news_file_ext;
		endforeach;		
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	/*About*/
	public function about($artist_id = NULL)
	{
		if(empty($artist_id)) redirect('');
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
				
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}
	
	/*Album*/
	public function album($artist_id=NULL,$album_id=NULL,$seo_text=NULL,$seo_text2=NULL)
	{
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);

		//Artist ID is not NULL
		if(empty($artist_id)) $this->page_missing();
		
		$this->data['dataAlbum'] = $this->db->select('k.*')
		->join('song_album as j','j.song_id=t.song_id','INNER')
		->join('album_info as k','k.album_id=j.album_id','INNER')
		->group_by('j.album_id')
		->order_by('j.album_id','DESC')	
		->get_where('song_artist as t',array('t.artist_id' => $artist_id))
		->result();
		if(!empty($album_id) && !empty($seo_text)):
			$this->data['dataAlbumRow'] = @$this->db->get_where('album_info',array('album_id' => $album_id),1)->row();
			$this->data['dataSongAlbum'] = $this->db->select('t.song_id,k.song_name,l.video_id,l.video_name')
			->join('video_song as j','j.song_id=t.song_id','LEFT')
			->join('song_info as k','k.song_id = t.song_id','INNER')
			->join('video_info as l','l.video_id = j.video_id','LEFT')
			->order_by('t.song_id','DESC')
			->get_where('song_album as t',"t.album_id = ".$album_id."")
			->result();
			$this->data['dataVideoAlbum'] = $this->db->select('t.video_id,k.video_name')
			->join('video_song as j','j.video_id=t.video_id','LEFT')
			->join('video_info as k','k.video_id = t.video_id','INNER')
			->order_by('j.video_id','DESC')
			->get_where('video_album as t',"t.album_id = ".$album_id." AND j.song_id IS NULL")
			->result();			
		endif;
		
		//Valid Review Song
		$is_exist = $this->db->from('news as t')
		->join('news_info as j','j.news_id=t.news_id','INNER')
		->where("t.news_type_id = 5 AND j.news_relate LIKE '%\"module\":\"Album\",\"value\":\"".$album_id."\"%'")
		->count_all_results();
		if(!empty($is_exist)):
			$this->data['dataAlbumReview'] = $this->db->select('t.news_id,j.news_subject')
			->join('news_info as j','j.news_id=t.news_id','INNER')
			->get_where('news as t',"t.news_type_id = 5 AND j.news_relate LIKE '%\"module\":\"Album\",\"value\":\"".$album_id."\"%'",1)
			->row();
		endif;		
				
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}	
	
	/*Song*/
	public function song($artist_id = NULL)
	{
		if(empty($artist_id)) redirect('');
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
		
		$this->data['dataResult'] = $this->db->select('k.*')
		->join('song_album as j','j.song_id=t.song_id','INNER')
		->join('album_info as k','k.album_id=j.album_id','INNER')
		->group_by('j.album_id')
		->order_by('j.album_id','DESC')	
		->get_where('song_artist as t',array('t.artist_id' => $artist_id))
		->result();
		
		foreach($this->data['dataResult'] as $value):
			$value->song = @$this->db->select('j.song_id,j.song_name')
			->join('song_info as j','j.song_id=t.song_id','INNER')
			->get_where('song_album as t',array('t.album_id' => $value->album_id))
			->result();
			foreach($value->song as $value2):
				$value2->video = $this->db->select('t.video_id,j.video_name')
				->join('video_info as j','j.video_id=t.video_id','INNER')
				->get_where('video_song as t',array('t.song_id' => $value2->song_id),1)
				->row();
			endforeach;			
		endforeach;
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);				
	}
	
	/*Video*/
	public function video($artist_id = NULL)
	{
		if(empty($artist_id)) redirect('');
		
		if($this->input->post('ajax')) $this->ajax_video_pager($artist_id,TRUE);
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
		
		$this->data['dataVideo'] = $this->ajax_video_pager($artist_id);
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}
	
	private function ajax_video_pager($artist_id,$ajax = FALSE)
	{
		/*Pager*/
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('video as t')
		->join('video_song as j','j.video_id=t.video_id','LEFT')
		->join('video_artist as k','k.video_id=t.video_id','LEFT')
		->join('song_artist as l','l.song_id=j.song_id','LEFT')
		->where("k.artist_id = ".$artist_id." OR l.artist_id = ".$artist_id."")
		->count_all_results();		
		$config['per_page'] = 20;
		$config['num_links'] = 10;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataVideo'] = $this->db->select('t.video_id,t.views,m.video_name,m.video_thumb_name,m.video_thumb_ext')
		->join('video_song as j','j.video_id=t.video_id','LEFT')
		->join('video_artist as k','k.video_id=t.video_id','LEFT')
		->join('song_artist as l','l.song_id=j.song_id','LEFT')
		->join('video_info as m','m.video_id=t.video_id','INNER')
		->order_by('t.video_id','DESC')
		->get_where('video as t',"k.artist_id = ".$artist_id." OR l.artist_id = ".$artist_id."",$config['per_page'],intval($this->input->get('p')))
		->result();
		
		$this->data['dataArtist'] = $this->db->select('artist_id,artist_name')
		->get_where('artist_info',array('artist_id' => $artist_id),1)
		->row();
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_video_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_video_pager", $this->data, TRUE);		
		endif;		
	}

	/*News*/
	public function news($artist_id = NULL)
	{
		if(empty($artist_id)) redirect('');
		
		if($this->input->post('ajax')) $this->ajax_news_pager($artist_id,TRUE);
		
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
		
		$this->data['dataNews'] = $this->ajax_news_pager($artist_id);
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
	}
	
	private function ajax_news_pager($artist_id,$ajax = FALSE)
	{
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('news_artist as t')
		->join('news as j','j.news_id=t.news_id','INNER')
		->where(array('t.artist_id' => $artist_id))
		->count_all_results();
		$config['per_page'] = 10;
		$config['num_links'] = 10;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataNews'] = $this->db->select('j.news_id,j.news_subject,j.news_brief')
		->join('news as k','k.news_id=t.news_id','INNER')	
		->join('news_info as j','j.news_id=t.news_id','INNER')	
		->order_by('j.news_id','DESC')	
		->get_where('news_artist as t',"t.artist_id = $artist_id AND k.is_publish = '1' AND (k.news_publish_date IS NULL OR k.news_publish_date < NOW())",$config['per_page'],intval($this->input->get('p')))
		->result();
		foreach($this->data['dataNews'] as $value):
			$dataImage = $this->db->order_by('news_image_order','ASC')->get_where('news_image',array('news_id' => $value->news_id),1)->row();
			$value->news_file_name = @$dataImage->news_file_name;
			$value->news_file_ext = @$dataImage->news_file_ext;
		endforeach;
		
		$this->data['dataArtist'] = $this->db->select('artist_id,artist_name')
		->get_where('artist_info',array('artist_id' => $artist_id),1)
		->row();
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_news_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_news_pager", $this->data, TRUE);		
		endif;		
	}	
	
	/*Gallery*/
	public function gallery($artist_id = NULL)
	{
		if(empty($artist_id)) redirect('');
		
		if($this->input->post('ajax')) $this->ajax_gallery_pager($artist_id,TRUE);	
		
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";			
		//Load Module
		$this->data['module'] = $this->module('artist');
		//Load Info Header
		$this->data['infoHeader'] = $this->infoHeader($artist_id);
		
		$this->data['dataGallery'] = $this->ajax_gallery_pager($artist_id);
		
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
	
	private function ajax_gallery_pager($artist_id,$ajax = FALSE)
	{			
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->db->from('artist_image')
		->where(array('artist_id' => $artist_id))
		->count_all_results();
		$config['per_page'] = 30;
		$config['num_links'] = 10;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataGallery'] = $this->db
		->order_by('artist_image_id','DESC')	
		->get_where('artist_image',array('artist_id' => $artist_id),$config['per_page'],intval($this->input->get('p')))
		->result();
		
		$this->data['dataArtist'] = $this->db->select('artist_id,artist_name')
		->get_where('artist_info',array('artist_id' => $artist_id),1)
		->row();
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_gallery_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_gallery_pager", $this->data, TRUE);		
		endif;		
	}				
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */